package com.jushu.storbox.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;

import com.jushu.storbox.util.LogUtil;

/***
 * 
 * Common Dialog
 */
public class ClouddriveDialog extends Dialog {
	private final static String LOG_TAG = "ClouddriveDialog";
	private int resourceId;
	private Context context;
	private int width = LayoutParams.WRAP_CONTENT;
	private int heigth = LayoutParams.WRAP_CONTENT;
	private View conventView;
	private boolean oncancleOutside = false;
	private Window window;

	/***
	 * 
	 * @param context
	 * @param theme
	 * @param resourceId
	 */
	public ClouddriveDialog(Context context, int theme, int resourceId) {
		super(context, theme);
		// TODO Auto-generated constructor stub
		LogUtil.i(LOG_TAG, "ClouddriveDialog");
		this.resourceId = resourceId;
		this.context = context;
		window = this.getWindow();
	}

	/***
	 * @param context
	 * @param resourceId
	 */
	public ClouddriveDialog(Context context, int resourceId) {
		super(context);
		LogUtil.i(LOG_TAG, "ClouddriveDialog");
		this.resourceId = resourceId;
		this.context = context;
	}

	/***
	 * conventView
	 * 
	 * @return View
	 */
	public View getConventView() {
		LogUtil.i(LOG_TAG, "getConventView");
		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		if (null == conventView) {
			conventView = mInflater.inflate(resourceId, null);
		}
		return conventView;
	}

	private void initInnerView() {
		LogUtil.i(LOG_TAG, "initInnerView");
		View conventView = getConventView();
		LayoutParams params = new LayoutParams(width, heigth);
		setContentView(conventView, params);
	}

	/***
	 * 
	 * @param width
	 *            int
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	public void setPosition(int x, int y) {
		window.setGravity(x | y);
	}
	

	/***
	 * @param heigth
	 *            int
	 */
	public void setHeigth(int heigth) {
		this.heigth = heigth;
	}

	@Override
	public void show() {
		initInnerView();
		super.show();
	}

	@Override
	public void setCanceledOnTouchOutside(boolean cancel) {
		this.oncancleOutside = cancel;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (MotionEvent.ACTION_DOWN == event.getAction() && oncancleOutside) {
			dismiss();
		}
		return super.onTouchEvent(event);
	}
}
